package zzhyc.keystore.sigmob

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.sigmob.windad.Splash.WindSplashAD
import com.sigmob.windad.Splash.WindSplashADListener
import com.sigmob.windad.Splash.WindSplashAdRequest
import com.sigmob.windad.WindAdError
import zzhyc.keystore.databinding.ActivitySigmobSplashBinding

private const val TAG = "SigmobSplashActivity"

class SigmobSplashActivity : AppCompatActivity() {

    private val binding by lazy { ActivitySigmobSplashBinding.inflate(layoutInflater) }
    private var canJump = false
    private var splashAD: WindSplashAD? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        Log.i(TAG, "onCreate: ")
        initSdk()
    }

    private fun initSdk() {
        val adRequest = WindSplashAdRequest(SigmobKey.SIGMOB_SPLASH_ID, "", null)
        splashAD = WindSplashAD(adRequest, object : WindSplashADListener {
            override fun onSplashAdShow(placementId: String?) {
                Log.i(TAG, "onSplashAdShow")
            }

            override fun onSplashAdLoadSuccess(placementId: String?) {
                Log.i(TAG, "onSplashAdLoadSuccess: ")
            }

            override fun onSplashAdLoadFail(error: WindAdError?, placementId: String?) {
                Log.i(TAG, "onSplashAdLoadFail: ${error?.errorCode}，msg-- ${error?.message}")
                startActivity()
            }

            override fun onSplashAdShowError(error: WindAdError?, placementId: String?) {
                Log.i(TAG, "onSplashAdShowError: ${error?.errorCode}，msg-- ${error?.message}")
                startActivity()
            }

            override fun onSplashAdClick(placementId: String?) {
                Log.i(TAG, "onSplashAdClick: ")
            }

            override fun onSplashAdClose(placementId: String?) {
                Log.i(TAG, "onSplashAdClose: ")
                startActivity()
            }

            override fun onSplashAdSkip(placementId: String?) {
                Log.i(TAG, "onSplashAdSkip: ")
                startActivity()
            }
        })
        splashAD?.loadAndShow(binding.sigmobSplashFl)
    }

    override fun onResume() {
        super.onResume()
        if (canJump) {
            startActivity()
        }
    }

    override fun onPause() {
        super.onPause()
        canJump = true
    }

    fun startActivity() {
        SigmobInfoActivity.startActivity(this)
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        splashAD?.destroy()
    }
}